package com.lily.aidouzixun.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 普通ListView的适配器的基类
 */
public class MyListAdapter extends BaseAdapter {
	private static final String TAG = "MyListAdapter";	
	private LayoutInflater inflater;
	private Handler handler;
	private int type;	//类型
	private Context context;
	
	private List<Object> data;
	private ViewHolderFactory mViewHolderFactory;	//Item上控件持有类

	/**
	 * 数据适配器的构造方法
	 * @param inflater LayoutInflater对象，用于加载试图
	 * @param type 类型
	 * @param context 上下文
     */
	public MyListAdapter(LayoutInflater inflater, int type,Context context) {
		super();
		this.inflater = inflater;
		this.type = type;
		this.context = context;
		mViewHolderFactory = new ViewHolderFactory();
	}
	
	public MyListAdapter(LayoutInflater inflater, Handler handler, int type,
			Context context) {
		super();
		this.inflater = inflater;
		this.handler = handler;
		this.type = type;
		this.context = context;
		mViewHolderFactory = new ViewHolderFactory();
	}
	
	/**
	 * 为适配器设置数据
	 * @param data
	 */
	public void setData(List<?> data){
		clearData();
		if(data == null){
			return;
		}
		if(this.data == null){
			this.data = new ArrayList<Object>();
		}
		
		Object[] dataObject = data.toArray();
		List<Object> dataList = Arrays.asList(dataObject);
		this.data.addAll(dataList);
	}
	/**
	 * 增加数据适配器中的数据
	 * @param Data
	 */
	public void addData(List<?> Data) {
		if (Data == null)
			return;
		if (this.data == null)
			this.data = new ArrayList<Object>();
		
		Object[] array = Data.toArray();
		List<Object> list = Arrays.asList(array);
		this.data.addAll(list);
	}
	
	/**
	 *得到数据适配器中的数据 
	 * @return
	 */
	public List<Object> getData() {
		return data;
	}
	
	/**
	 * 清理适配器的数据
	 */
	public void clearData(){
		if(data != null){
			data.clear();
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(data == null) {
			return 0;
		}
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if(data == null){
			return null;
		}
		if(position <0 || position >= data.size()){
			return null;
		}
		return data.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(data == null){
			return null;
		}
		if(position < 0 || position >= data.size()){
			return null;
		}
		MyViewHolder mViewHolder = null;
		if(convertView == null && mViewHolderFactory != null){
			//得到Item布局
			convertView = mViewHolderFactory.createConvertView(inflater,type);
			//得到Item布局上控件持有类
			mViewHolder = mViewHolderFactory.createViewHolder(convertView,type,context);
			if(convertView != null) {
				convertView.setTag(mViewHolder);
			}
		}else{
			mViewHolder = (MyViewHolder) convertView.getTag();
		}
		//L.v(TAG, "position-->" + position);
		if(mViewHolder != null ){
			mViewHolder.setInfo(data.get(position),type);
		}
		return convertView;
	}

}
